/**
 * Copyright (c) 2020, Mr.Wang (recallcode@aliyun.com) All rights reserved.
 */

package com.cpipec.demo.todo.impl;

import cn.hutool.db.Entity;
import cn.hutool.db.Session;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cpipec.demo.spi.BPMServerRestClient;
import com.cpipec.demo.todo.TodoService;
import com.cpipec.demo.vo.PageResultVO;
import com.cpipec.demo.vo.TrainVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TodoServiceImpl implements TodoService {

	private static Logger logger = LoggerFactory.getLogger(TodoServiceImpl.class);

	@Autowired
	private DataSource dataSource;

	@Autowired
	private BPMServerRestClient bpmServerRestClient;

	@Override
	public PageResultVO<Entity> getTodoList(String username, int page, int pageSzie) {
		String bpmResult = bpmServerRestClient.getPendingTaskList(username, page, pageSzie);
		int count = JSONObject.parseObject(bpmResult).getJSONObject("ObjRetVal").getIntValue("recordCount");
		JSONArray dataItems = JSONObject.parseObject(bpmResult).getJSONObject("ObjRetVal").getJSONArray("dataItems");
		List<Entity> entityList = new ArrayList<Entity>();
		dataItems.forEach(dataItem -> {
			Entity entity = new Entity();
			entity.set("taskId", JSONObject.parseObject(dataItem.toString()).getString("PENDINGCODE"));
			entity.set("bpmName", JSONObject.parseObject(dataItem.toString()).getString("PENDINGPROCESSNAME"));
			entity.set("instanceId", JSONObject.parseObject(dataItem.toString()).getString("PENDINGINCIDENT"));
			entity.set("stepName", JSONObject.parseObject(dataItem.toString()).getString("PENDINGSTEPNAME"));
			entity.set("todoName", JSONObject.parseObject(dataItem.toString()).getString("PENDINGOWNERNAME"));
			entity.set("todoNameCode", JSONObject.parseObject(dataItem.toString()).getString("PENDINGOWNER"));
			entity.set("startTime", JSONObject.parseObject(dataItem.toString()).getString("PENDINGDATE"));
			entity.set("endTime", JSONObject.parseObject(dataItem.toString()).getString("LASTUPDATEDATE"));
			entity.set("sourceName", JSONObject.parseObject(dataItem.toString()).getString("PENDINGSOURCEUSERNAME"));
			entity.set("sourceNameCode", JSONObject.parseObject(dataItem.toString()).getString("PENDINGSOURCEUSERID"));
			entity.set("tableName", JSONObject.parseObject(dataItem.toString()).getString("RESERVED_1"));
			entity.set("sourceId", JSONObject.parseObject(dataItem.toString()).getString("RESERVED_2"));
			entity.set("taskName", JSONObject.parseObject(dataItem.toString()).getString("RESERVED_3"));
			entity.set("startName", JSONObject.parseObject(dataItem.toString()).getString("RESERVED_4"));
			entity.set("startNameCode", JSONObject.parseObject(dataItem.toString()).getString("PENDINGFORMID"));
			entityList.add(entity);
		});
		return new PageResultVO<Entity>(entityList, count);
	}

	@Override
	public boolean submitTode(TrainVO trainVO) {
		Session session = Session.create(dataSource);
		try {
			Entity entity = Entity.create("train_project").set("bmyj", trainVO.getBmyj()).set("rlzyyj", trainVO.getRlzyyj()).set("gsldyj", trainVO.getGsldyj());
			Entity where = Entity.create("train_project").set("id", trainVO.getId());
			session.beginTransaction();
			String bpmResult = bpmServerRestClient.submitTaskStep(trainVO);
			String value = JSONObject.parseObject(bpmResult).getString("Result");
			if ("1".equals(value)) {
				session.update(entity, where);
				session.commit();
				return true;
			} else {
				throw new RuntimeException("提交待办任务失败");
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			session.quietRollback();
			return false;
		}
	}
}
